package com.IR.product.service;


import java.util.List;
import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import com.IR.shop.mapper.ProductMapper;
import com.IR.shop.mapper.ProtypeMapper;
import com.IR.shop.po.Product;
import com.IR.shop.po.ProductExample;


/**
 * @author 单立鑫
 * @date 2018-09-05
 * */


@Service
public class BProductBIZ {
	
	@Resource
	private ProductMapper promapper;
	@Resource
	private ProtypeMapper protypemapper;
	
	/*查询下架*/
	public List<Product> showProDown(Integer pagenow,Integer pagesize){
		ProductExample proexample=this.fenyeDown(pagenow, pagesize);
		ProductExample.Criteria c=proexample.createCriteria();
		c.andIsproEqualTo(0);
		return promapper.selectByExample(proexample);
	}
	
	/*通过商品id查询*/
	public Product showProId(Long proid){		
		return promapper.selectByPrimaryKey(proid);
	}
	
	/*查询商品种类id展示商品*/
	public List<Product> showProType(Integer pagenow,Integer pagesize,int protypeid){
		ProductExample proexample =this.fenyeUp(pagenow, pagesize);
		ProductExample.Criteria c=proexample.createCriteria();
		c.andIsproEqualTo(1);
		c.andProtypeidEqualTo(protypeid);
		return promapper.selectByExample(proexample);
	}
	
	/*分页查询*/
	public List<Product> showProPage(Integer pagenow,Integer pagesize){	
		ProductExample proexample=this.fenyeUp(pagenow, pagesize);
		ProductExample.Criteria c=proexample.createCriteria();
		c.andIsproEqualTo(1);
		return promapper.selectByExample(proexample);
	}
	
	/*下面两个方法为了方便代码复用单独写的方法，一个上架商品使用的，一个下架商品使用的能够直接返回example对象*/
	
	/*上架商品使用的分页查询*/
	private ProductExample fenyeUp(Integer pagenow,Integer pagesize) {
		Integer start=(pagenow-1)*pagesize;//初始条数
		ProductExample proexample=new ProductExample();
		proexample.setOffset(start);
		proexample.setLimit(pagesize);
		return proexample;
	}
	
	/*下架商品使用的分页查询*/
	private ProductExample fenyeDown(Integer pagenow,Integer pagesize) {
		Integer start=(pagenow-1)*pagesize;//初始条数
		ProductExample proexample=new ProductExample();
		proexample.setOffset(start);
		proexample.setLimit(pagesize);
		return proexample;
	}	
	/*附近商品查询*/
	public List<Product> showProsite(String prosite,int protypeid){
		ProductExample example=this.fenyeUp(1, 4);
		ProductExample.Criteria c=example.createCriteria();
		c.andPrositeLike("%"+prosite+"%");
		c.andIsproEqualTo(1);
		c.andProtypeidEqualTo(protypeid);
		return promapper.selectByExample(example);
	}
	/*商品搜索查询*/
	public List<Product> showProsearch(String proname,int pagenow,int pagesize){
		ProductExample example=new ProductExample();
		ProductExample.Criteria c=example.createCriteria();
		example.setOffset((pagenow-1)*pagesize);
		example.setLimit(pagesize);
		c.andPronameLike("%"+proname+"%");
		c.andIsproEqualTo(1);
		return promapper.selectByExample(example);
	}
}

